home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.05 May 96 / Java < prev   
Encoding:
Text File  |  1996-04-19  |  10.5 KB  |  303 lines  |  [TEXT/R*ch]

  1. Our first Java application will display the obligatory “Hello, world!”  message:
  2.  
  3. public class hello
  4. {
  5.     public static void main(String argv[])
  6.     {
  7.         System.out.println("Hello, world!");
  8.     }
  9. }
  10.  
  11. Create a new file, type in this code, then save the file as hello.java.  Just as
  12. you’d end your C source code file with the extension .c and your C++ source code
  13. file with .cp or .cpp, the extension .java is used to denote a Java source code
  14. file.
  15. If you are using the JDK, drag the file hello.java onto the application named
  16. Java Compiler (gee, guess what this is).  The compiler will convert your source
  17. code into Java byte code, intended for a Java interpreter that will turn these
  18. byte codes into the instructions specific to the machine it is running on.  The
  19. interpreter is part of the virtual machine, the layer that lies between the
  20. platform-independent Java byte code and your machine.
  21. The compiler writes the Java byte code into a file named xxx.class, where xxx is
  22. the name of the class you’ve implemented.  In this case, the compiler will
  23. create a file called hello.class.  Again, if you are using the JDK, drag the
  24. file hello.class onto the application named Applet Viewer.  As you might expect,
  25. the Applet Viewer will start running at the public method named main().
  26. The output of this application is shown in Figure 1.  Notice that the window is
  27. named stdout.  If you’ve ever spent any time in the Unix universe, you know that
  28. stdout stands for “standard output”.  The method System.out.println() sends its
  29. output to the stdout window followed by a carriage return.  The method
  30. System.out.print() sends its output to the stdout window without generating a
  31. carriage return.
  32.  
  33. Figure 1.  The output from the hello.class application
  34. System.out.println() is a lot like printf() or cout in that they all send their
  35. output to a console window.  Just as you first learned to program using consoles
  36. and eventually moved to the Mac Toolbox for your user interface, we’ll start
  37. with System.out.println() and eventually move on to the user interface routines
  38. in the AWT (advanced windowing toolkit).  You’ll use the AWT to implement a user
  39. interface you’ll want to appear on a web page.
  40. Don’t worry too much about the structure of our Java source code just yet.  The
  41. three applications in this month’s column all use the same basic structure: a
  42. class wrapper with a single public method called main().  Oh yeah, in Java, a
  43. class’ functions are called methods instead of member functions.
  44. Your Second Application: StringTester
  45. Our second application, stringTester, introduces an important Java data type:
  46. String.  As its name suggests, the String class was implemented to work with
  47. strings.  Unlike C and Pascal strings, a Java String is an object, complete with
  48. variables (the Java term for data members) and methods.  Our third applica-tion,
  49. stringMethods, will demonstrate some of the String methods.  This application,
  50. stringTester, will get us started.
  51. Here’s the source...
  52.  
  53. public class stringTester
  54. {
  55.     public static void main(String argv[])
  56.     {
  57.         String    string1, string2 = ", world!";
  58.         
  59.         string1 = "Hello";
  60.         
  61.         System.out.print( string1 );
  62.         System.out.println( string2 );
  63.         System.out.println( string1 + string2 );
  64.         
  65.         string1 += string2;
  66.         System.out.println( string1 );
  67.         
  68.         System.out.println( "Length of this string: " + 
  69.                             string1.length() );
  70.     }
  71. }
  72.  
  73. The first few lines show you two ways to create and initialize a String:
  74.  
  75.         String    string1, string2 = ", world!";
  76.         
  77.         string1 = "Hello";
  78.  
  79. You can initialize the String when you define it or you can use the assignment
  80. operator, as we did in the second line.  Nothing unusual here.
  81. The next line prints the first string, “Hello”, without a carriage return.  The
  82. second line prints the second string, “, world!”, with a carriage return.
  83.  
  84.         System.out.print( string1 );
  85.         System.out.println( string2 );
  86.  
  87. This produces this line of output in the stdout window:
  88.  
  89. Hello, world!
  90.  
  91. The next line uses the + operator to concatenate string1 and string2, sending
  92. the joined string as a parameter to println().
  93.  
  94.         System.out.println( string1 + string2 );
  95.  
  96. Here’s the output produced by this code:
  97.  
  98. Hello, world!
  99.  
  100. Next, the += operator is used to concatenate string2 onto the end of string1 and
  101. the new string1 is send to stdout:
  102.  
  103.         string1 += string2;
  104.         System.out.println( string1 );
  105.  
  106. Once again, here is the output:
  107.  
  108. Hello, world!
  109.  
  110. Finally, the length() method is called to return the length of the modified
  111. string1.  Notice that the + operator is used to merge the two strings passed to
  112. println() into a single string:
  113.  
  114.         System.out.println( "Length of this string: " + 
  115.                             string1.length() );
  116.  
  117. Here’s the final line of output:
  118.  
  119. Length of this string: 13
  120. Your Third Application: StringMethods
  121. Our last application this month demonstrates some of the String methods.  As you
  122. go through this program, take a moment to go through the documentation that came
  123. with your development environment.  In particular, look for the file
  124. java.lang.String.html.  As you’ll see as you learn more about Java, all the Java
  125. classes are part of some larger collection of classes.  These collections take
  126. the form of packages.  For example, the String class (along with the rest of the
  127. “built-in” Java types) are part of the java.lang package.  To use a package, you
  128. use a mechanism similar to the #include.  This mechanism is the import
  129. statement.  We’ll learn about the import statement in next month’s column.  The
  130. one package you automatically have access to is java.lang and so you don’t need
  131. to import it to get access to the String class.
  132. The file java.lang.String.html contains a complete description of the variables
  133. and methods that make up the String class.  Use your Web browser to open this
  134. html file and look over the class.
  135. Here’s the stringMethods source code...
  136.  
  137. public class stringMethods
  138. {
  139.     public static void main(String argv[])
  140.     {
  141.         char    myArray[] = {'a', 'b', 'c', 'd', 'e'};
  142.         java.lang.String    string = new String( myArray );
  143.         
  144.         System.out.println( "string: " + string );
  145.         System.out.println( "string[2]: "
  146.                 + string.charAt( 2 ) );
  147.         
  148.         string = string.concat( string );
  149.         System.out.println( "Doubled string: "
  150.                 + string );
  151.         
  152.         System.out.println( "Index of first 'x': "
  153.                 + string.indexOf( 'x' ) );
  154.         
  155.         int    index = string.indexOf( 'e' );
  156.         System.out.println( "Index of first 'e': "
  157.                 + index );
  158.         
  159.         if ( index >= 0 )
  160.             System.out.println( "Index of second 'e': "
  161.                 + string.indexOf( 'e', index+1 ) );
  162.         
  163.         System.out.println( "substring[2] to the end: "
  164.                 + string.substring( 2 ) );
  165.         System.out.println( "substring[2] up to string[4]: "
  166.                 + string.substring( 2, 4 ) );
  167.         
  168.         string = string.replace( 'c', 'x' );
  169.         System.out.println( "Replace 'c' with 'x': "
  170.                 + string );
  171.         
  172.         System.out.println( "Display as upper case: "
  173.                 + string.toUpperCase() );
  174.     }
  175. }
  176.  
  177. The first two lines show you yet another way to create a new String.  As you’ll
  178. see when you read through java.lang.String.html, there are several versions of
  179. the String constructor.  Just like C++, Java supports function overloading,
  180. allowing you to create multiple versions of the same function, as long as each
  181. version has a unique signature (the function name combined with the parameter
  182. list).
  183. This line defines an array of chars and initializes the array with the
  184. characters a through e:
  185.  
  186.         char    myArray[] = {'a', 'b', 'c', 'd', 'e'};
  187.  
  188. This line uses new to define the new String object, using the char array to
  189. initialize the String to the string “abcde”.  Notice that we refer to
  190. java.lang.String instead of just String.  The two terms are equivalent.  Since
  191. the java.lang package is automatically included, the java.lang prefix is not
  192. needed.
  193.  
  194.         java.lang.String    string = new String( myArray );
  195.  
  196. The next line prints this string.  System is actually java.lang.System. 
  197. java.lang.System features a variable called out which features methods called
  198. print() and println().  We could have referred to java.lang.System.out.println()
  199. but, again, the java.lang is assumed.
  200.  
  201.         System.out.println( "string: " + string );
  202.  
  203. Here’s the output:
  204.  
  205. string: abcde
  206.  
  207. This next println() calls the String method charAt().  charAt() returns the nth
  208. character in the String.
  209.  
  210.         System.out.println( "string[2]: "
  211.                 + string.charAt( 2 ) );
  212.     
  213. Here’s the output.  Note that Java strings are 0-based, just like C and C++.
  214.  
  215. string[2]: c
  216.  
  217. The concat() method appends its parameter to the end of the current object.  In
  218. this case, we concat() string on the end of string, storing the result in
  219. string, then print the newly doubled string.
  220.  
  221.         string = string.concat( string );
  222.         System.out.println( "Doubled string: "
  223.                 + string );
  224.  
  225. Here’s the output...
  226.  
  227. Doubled string: abcdeabcde
  228.  
  229. The method indexOf() searches the string for the specified character, returning
  230. either an index into the string or the value -1.
  231.  
  232.         System.out.println( "Index of first 'x': "
  233.                 + string.indexOf( 'x' ) );
  234.  
  235. Here’s the output.  Since the string doesn’t contain an ‘x’, indexOf() returned
  236. -1.
  237.  
  238. Index of first 'x': -1
  239.  
  240. The next lines of code searches for the first ‘e’ in the String, storing the
  241. index in the variable index, then printing the index.
  242.  
  243.         int    index = string.indexOf( 'e' );
  244.         System.out.println( "Index of first 'e': "
  245.                 + index );
  246. Here’s the output:
  247.  
  248. Index of first 'e': 4
  249.  
  250. Next, assuming the index was not negative, we use a second version of indexOf()
  251. which takes a second parameter.  This second parameter tells indexOf() where to
  252. start its search in the string, allowing us to search the string for a second
  253. ‘e’.
  254.  
  255.         if ( index >= 0 )
  256.             System.out.println( "Index of second 'e': "
  257.                 + string.indexOf( 'e', index+1 ) );
  258.  
  259. Here’s the output:
  260.  
  261. Index of second 'e': 9
  262.  
  263. Next, the substring() method is called.  substring() takes an index into the
  264. string and returns a string that runs from the index to the end of the string.
  265.  
  266.         System.out.println( "substring[2] to the end: "
  267.                 + string.substring( 2 ) );
  268.  
  269. Here’s the output:
  270.  
  271. substring[2] to the end: cdeabcde
  272.  
  273. Next, we call a second version of substring().  This one takes a second
  274. parameter, an index that marks the end of the substring.
  275.  
  276.         System.out.println( "substring[2] up to string[4]: "
  277.                 + string.substring( 2, 4 ) );
  278.  
  279. Here’s the output:
  280.  
  281. substring[2] up to string[4]: cd
  282.  
  283. replace() replaces all occurrences of char 1 with char 2 in the string, then
  284. prints the result.
  285.  
  286.         string = string.replace( 'c', 'x' );
  287.         System.out.println( "Replace 'c' with 'x': "
  288.                 + string );
  289.  
  290. Here’s the result:
  291.  
  292. Replace 'c' with 'x': abxdeabxde
  293.  
  294. toUpperCase() replaces all lower case letters in the string with their upper
  295. case equivalents.
  296.  
  297.         System.out.println( "Display as upper case: "
  298.                 + string.toUpperCase() );
  299.  
  300. Here’s the result:
  301.  
  302. Display as upper case: ABXDEABXDE
  303.